<?php
namespace App\Http\Controllers;

use App\Handler\BulletinHandler;
use App\Models\Bulletin;
use App\Http\C5\C5ResponseFactory;
use App\Models\BulletinContent;
use App\Utils\FileUtils;
use App\Utils\ResourceUtils;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;

class BulletinController extends BaseController
{

    public function edit()
    {
        $id = $this->get("bulletin_id");
        //如有需要这里加上排重的功能
        if (empty($id)) {
            return $this->create();
        } else {
            return $this->update();
        }
    }

    public function create()
    {
        $bean = new Bulletin();
        $this->markCreateInfo($bean);
        $bean->forceFill($this->request->all());
        $bean->save();
        $this->copyImageFile($bean);
        BulletinHandler::saveContent($bean->get_bulletin_id(), $this->get("content"), true);
        return C5ResponseFactory::newSuccessInstance($bean);
    }


    public function copyImageFile($bean)
    {
        /**
         * @var $bean Bulletin
         */
        $uploadRoot = env("UI_UPLOAD_ROOT", "d:/temp/");
        $targetRoot = env("UI_SITE_ROOT", "d:/temp/") . "app/files/";
        $file = $bean->get_image_save_name();
        $dir = FileUtils::get_file_path($file);
        if (!file_exists($targetRoot . $dir)) {
            mkdir($targetRoot . $dir, 0777, true);
        }
        $fromFile = $uploadRoot . $bean->get_image_save_name();
        Log::debug("copy $fromFile to $targetRoot ");
        if (!is_file($fromFile)) {
            return;
        }
        $targetFile = $targetRoot . $bean->get_image_save_name();
        copy($fromFile, $targetFile);
    }


    public function update()
    {
        /**
         * @var  $bean Bulletin
         */
        $bulletin_id = $this->get("bulletin_id");
        $bean = Bulletin::find($bulletin_id);
        if (empty($bean)) {
            return C5ResponseFactory::newRecordNotExistsInstance();
        }
        $args = $this->request->all();
        $this->markUpdateInfo($bean);
        $bean->forceFill($args);
        $bean->update();
        $this->copyImageFile($bean);
        BulletinHandler::saveContent($bean->get_bulletin_id(), $this->get("content"), true);
        return C5ResponseFactory::newSuccessInstance($bean);
    }

    public function delete()
    {
        /**
         * @var  $bean Bulletin
         */

        $bulletin_id = $this->get("bulletin_id");
        $bean = Bulletin::find($bulletin_id);
        if (empty($bean)) {
            return C5ResponseFactory::newSuccessInstance($bulletin_id);
        }
        // 检查数据完整性,如果有被使用就不可以删除

        // $object = SomeModel::where("bulletin_id", $bulletin_id)->first();
        // if (!empty($object)) {
        //      return C5ResponseFactory::newRecordUsedInstance();
        // }

        $bean->delete();
        // 删除关联的数据
        BulletinContent::where("bulletin_id", $bulletin_id)->delete();
        return C5ResponseFactory::newSuccessInstance($bulletin_id);
    }

    public function detail()
    {
        $id = $this->get("bulletin_id");
        return $this->find($id);
    }

    public function find($bulletin_id)
    {
        /**
         * @var  $bean Bulletin
         */
        $bean = Bulletin::find($bulletin_id);
        if (empty($bean)) {
            return C5ResponseFactory::newRecordNotExistsInstance();
        }
        $result = $bean->toArray();
        $result["content"] = BulletinHandler::loadContent($bulletin_id);
        return C5ResponseFactory::newSuccessInstance($result);
    }

    public function grid()
    {
        $oid = $this->currentOrganizationId();
        $limits = empty($oid) ? "" : " and a.organization_id=$oid ";
        $sql = "
            select a.* from bulletin a
            where 1=1
            $limits
        ";
        $sql .= $this->getQueryLimits();
        $sql .= $this->getOrderBy(["from_time" => "desc"]);
        Log::debug($sql);
        $result = $this->gridQuery($sql);
        $list = json_decode(json_encode($result), true);
        ResourceUtils::toInternets($list, "image_save_name");
        return C5ResponseFactory::newSuccessInstance($list);
    }


}

